<template>
	<view>
		
	</view>
</template>

<script>
	import {
		mapState
	} from "vuex";
	import axios from 'axios'
	import {
		BASEURL
	} from '@/constant/index.js'
	import {
		checkPayStatus
	} from '@/service/index'

	export default {
		data() {
			return {
				params: {},
				timer: '', // 倒计时
			};
		},
		computed: {
			...mapState({
				userInfo: state => state.login.userInfo
			}),
		},
		onReady() {
			uni.setNavigationBarTitle({
				title: '支付中'
			});
		},
		onLoad(options) {
			console.log(options)
			if (options.key === 'pc' && options.token) {
				this.params = options
				this.pay()
			}
		},
		methods: {
			pay() {
				uni.showLoading({
					title: '支付中...'
				})
				if (this.params.content_id) {
					this.payProgramme()
				} else {
					this.payRecharge()
				}
			},
			payProgramme() {
				let _this = this
				axios({
					url: `${BASEURL}/api/member.Member/buy`,
					method: 'post',
					data: {
						content_id: this.params.content_id,
						sale_id: this.params.sale_id,
						pay_type: this.params.pay_type,
						coupon_id: this.params.coupon_id
					},
					headers: {
						'Content-Type': undefined,
						'Apitoken': this.params.token
					}
				}).then((response) => {
					uni.hideLoading();
					console.log('response: ', response)
					if (response.status === 200) {
						if (response.data.code === 200) {
							if (response.data.data.order.pay_status) {
								uni.showToast({
									title: '方案已解锁',
									icon: 'none'
								})
								setTimeout(() => {
									window.location.href = `${decodeURIComponent(_this.params.redirect_uri)}/user/#/pages/details/details?appid=${_this.params.appid}&content_id=${_this.params.content_id}&other=${new Date().getTime()}`
								}, 500)
								return
							}
							if (this.params.pay_type == 1) {
								WeixinJSBridge.invoke('getBrandWCPayRequest', response.data.data.pay_sdk, function(res) {
									//res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
									if (res.err_msg == "get_brand_wcpay_request:ok") {
										uni.showLoading({
											title: '支付查询中...'
										})
										// 轮询监测支付回调状态
										_this.timer = setInterval(function() {
											axios({
												url: `${BASEURL}/api/member.Member/checkPayStatus`,
												method: 'post',
												data: {
													order_no: response.data.data.order.order_no
												},
												headers: {
													'Content-Type': undefined,
													'Apitoken': _this.params.token
												}
											}).then((result) => {
												if (result.status === 200) {
													if (result.data.code === 200) {
														if (result.data.data.pay_status == 1) {
															// 跳转境外
															window.location.href = `${decodeURIComponent(_this.params.redirect_uri)}/user/#/pages/details/details?appid=${_this.params.appid}&content_id=${_this.params.content_id}&other=${new Date().getTime()}`
															clearInterval(_this.timer)
															uni.hideLoading()
														}
													} else {
														_this.$showToast('支付异常')
														clearInterval(_this.timer)
														uni.hideLoading();
													}
												} else {
													_this.$showToast('网络异常')
													clearInterval(_this.timer)
													uni.hideLoading();
												}
											}).catch(error => {
												clearInterval(_this.timer)
												uni.hideLoading();
											})
										}, 1000);
									} else {
										uni.showLoading({
											title: '取消支付,正在转回方案详情...'
										})
										setTimeout(async () => {
											window.location.href = `${decodeURIComponent(_this.params.redirect_uri)}/user/#/pages/details/details?appid=${_this.params.appid}&content_id=${_this.params.content_id}&other=${new Date().getTime()}`
										}, 1500)
									}
								});
							}
							if (this.params.pay_type == 2) {
								uni.navigateTo({
									url: `/pages/ali_pay/ali_pay?appid=${this.params.appid}&content_id=${this.params.content_id}&order_no=${response.data.data.order.order_no}&token=${this.params.token}&redirect_uri=${this.params.redirect_uri}&other=${new Date().getTime()}`
								});
							}
						} else {
							this.$showToast('发起支付失败')
						}
					} else {
						this.$showToast('网络异常')
					}
				}).catch(error => {
					uni.hideLoading();
				})
			},
			payRecharge() {
				let _this = this
				axios({
					url: `${BASEURL}/api/member.Recharge/pay`,
					method: 'post',
					data: {
						amount_id: this.params.amount_id,
						pay_type: this.params.pay_type
					},
					headers: {
						'Content-Type': undefined,
						'Apitoken': this.params.token
					}
				}).then((response) => {
					uni.hideLoading();
					console.log('response: ', response)
					if (response.status === 200) {
						if (response.data.code === 200) {
							if (this.params.pay_type == 1) {
								WeixinJSBridge.invoke('getBrandWCPayRequest', response.data.data.pay_sdk, function(res) {
									//res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
									if (res.err_msg == "get_brand_wcpay_request:ok") {
										uni.showLoading({
											title: '支付查询中...'
										})
										// 轮询监测支付回调状态
										_this.timer = setInterval(function() {
											axios({
												url: `${BASEURL}/api/member.Recharge/info`,
												method: 'post',
												data: {
													order_no: response.data.data.order.order_no
												},
												headers: {
													'Content-Type': undefined,
													'Apitoken': _this.params.token
												}
											}).then((result) => {
												if (result.status === 200) {
													if (result.data.code === 200) {
														if (result.data.data.pay_status == 1) {
															// 跳转境外
															window.location.href = `${decodeURIComponent(_this.params.redirect_uri)}/user/#/pages/user/payBalance/payBalance/?appid=${_this.params.appid}`
															clearInterval(_this.timer)
															uni.hideLoading()
														}
													} else {
														_this.$showToast('支付异常')
														clearInterval(_this.timer)
														uni.hideLoading();
													}
												} else {
													_this.$showToast('网络异常')
													clearInterval(_this.timer)
													uni.hideLoading();
												}
											}).catch(error => {
												clearInterval(_this.timer)
												uni.hideLoading();
											})
										}, 1000);
									} else {
										uni.showLoading({
											title: '取消支付,正在转回充值页面...'
										})
										setTimeout(async () => {
											window.location.href = `${decodeURIComponent(_this.params.redirect_uri)}/user/#/pages/user/payBalance/payBalance/?appid=${_this.params.appid}`
										}, 1500)
									}
								});
							}
							if (this.params.pay_type == 2) {
								uni.navigateTo({
									url: `/pages/ali_pay/ali_pay?appid=${this.params.appid}&content_id=&order_no=${response.data.data.order.order_no}&token=${this.params.token}&redirect_uri=${this.params.redirect_uri}&other=${new Date().getTime()}`
								});
							}
						} else {
							this.$showToast('发起支付失败')
						}
					} else {
						this.$showToast('网络异常')
					}
				}).catch(error => {
					uni.hideLoading();
				})
			}
		}
	}
</script>

<style lang="scss">

</style>